package com.yuehuanghun.admin.generator.domain;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Transient;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

import org.apache.commons.lang3.ArrayUtils;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonFormat.Shape;
import com.yuehuanghun.admin.core.config.MyExampleQueryConverter;
import com.yuehuanghun.admin.generator.config.GenConstants;
import com.yuehuanghun.framework.util.StringUtils;
import com.yuehuanghun.mybatis.milu.annotation.AttributeOptions;
import com.yuehuanghun.mybatis.milu.annotation.ExampleQuery;
import com.yuehuanghun.mybatis.milu.annotation.ExampleQuery.MatchType;
import com.yuehuanghun.mybatis.milu.tool.Constants;
import com.yuehuanghun.mybatis.milu.annotation.Filler;

import lombok.Data;

/**
 * 业务表 gen_table
 * 
 * @author yadmin
 */
@Entity
@Data
public class GenTable implements Serializable {
	private static final long serialVersionUID = 1L;

	/** 编号 */
	@Id
	@GeneratedValue(generator = Constants.ID_GENERATOR_SNOWFLAKE)
	@JsonFormat(shape = Shape.STRING)
	private Long tableId;

	/** 表名称 */
	@NotBlank(message = "表名称不能为空")
	@AttributeOptions(exampleQuery = @ExampleQuery(matchType = MatchType.CONTAIN))
	private String tableName;

	/** 表描述 */
	@NotBlank(message = "表描述不能为空")
	@AttributeOptions(exampleQuery = @ExampleQuery(matchType = MatchType.CONTAIN))
	private String tableComment;

	/** 关联父表的表名 */
	private String subTableName;

	/** 本表关联父表的外键名 */
	private String subTableFkName;

	/** 实体类名称(首字母大写) */
	@NotBlank(message = "实体类名称不能为空")
	private String className;

	/** 使用的模板（crud单表操作 tree树表操作 sub主子表操作） */
	private String tplCategory;

	/** 生成包路径 */
	@NotBlank(message = "生成包路径不能为空")
	private String packageName;

	/** 生成模块名 */
	@NotBlank(message = "生成模块名不能为空")
	private String moduleName;

	/** 生成业务名 */
	@NotBlank(message = "生成业务名不能为空")
	private String businessName;

	/** 生成功能名 */
	@NotBlank(message = "生成功能名不能为空")
	private String functionName;

	/** 生成作者 */
	@NotBlank(message = "作者不能为空")
	private String functionAuthor;

	/** 生成代码方式（0zip压缩包 1自定义路径） */
	private String genType;

	/** 生成路径（不填默认项目路径） */
	private String genPath;

	/** 其它生成选项 */
	private String options;

	/** 创建者 */
	private String createBy;

	/** 创建时间 */
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	@AttributeOptions(filler = @Filler(fillOnInsert = true), exampleQuery = {@ExampleQuery(startKeyName = "params.beginTime", endKeyName = "params.endTime", valueConverter = MyExampleQueryConverter.class)})
	private Date createTime;

	/** 更新者 */
	private String updateBy;

	/** 更新时间 */
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	@AttributeOptions(filler = @Filler(fillOnInsert = true, fillOnUpdate = true))
	private Date updateTime;

	/** 备注 */
	private String remark;

	/** 主键信息 */
	@Transient
	private GenTableColumn pkColumn;

	/** 子表信息 */
	@Transient
	private GenTable subTable;

	/** 表列信息 */
	@Valid
	@Transient
	private List<GenTableColumn> columns;

	/** 树编码字段 */
	@Transient
	private String treeCode;

	/** 树父编码字段 */
	@Transient
	private String treeParentCode;

	/** 树名称字段 */
	@Transient
	private String treeName;

	/** 上级菜单ID字段 */
	@Transient
	private String parentMenuId;

	/** 上级菜单名称字段 */
	@Transient
	private String parentMenuName;
	
	/** 请求参数 */
	@Transient
    private Map<String, Object> params;
	
	public Map<String, Object> getParams() {
		return params == null ? (params = new HashMap<>()) : params;
	}
	
	public boolean isSub() {
		return isSub(this.tplCategory);
	}

	public static boolean isSub(String tplCategory) {
		return tplCategory != null && StringUtils.equals(GenConstants.TPL_SUB, tplCategory);
	}

	public boolean isTree() {
		return isTree(this.tplCategory);
	}

	public static boolean isTree(String tplCategory) {
		return tplCategory != null && StringUtils.equals(GenConstants.TPL_TREE, tplCategory);
	}

	public boolean isCrud() {
		return isCrud(this.tplCategory);
	}

	public static boolean isCrud(String tplCategory) {
		return tplCategory != null && StringUtils.equals(GenConstants.TPL_CRUD, tplCategory);
	}

	public boolean isSuperColumn(String javaField) {
		return isSuperColumn(this.tplCategory, javaField);
	}

	public static boolean isSuperColumn(String tplCategory, String javaField) {
		if (isTree(tplCategory)) {
			return StringUtils.equalsAnyIgnoreCase(javaField,
					ArrayUtils.addAll(GenConstants.TREE_ENTITY, GenConstants.BASE_ENTITY));
		}
		return StringUtils.equalsAnyIgnoreCase(javaField, GenConstants.BASE_ENTITY);
	}
}